import gnomevfs
import nautilus

MISSING = object()
WF_NS = 'http://cmf.zope.org/propsets/dcworkflow'
DC_NS = 'http://cmf.zope.org/propsets/dublincore'

def set_column_value(handle, result, op_data, data):
    rsrc, ps = op_data
    file, pname, cname = data
    prop = ps.getProperty(pname, MISSING)
    if prop is MISSING:
        return
    file.add_string_attribute(cname, unicode(prop.getValue()))

def open_cb(handle, exc, data):
    if exc is None:
        operation_data, callback_data = data[0], data[1:]
        handle.control(operation='plone:propertyset',
                       operation_data=operation_data,
                       callback=set_column_value,
                       callback_data=callback_data)

class WorkflowStatusColumn(nautilus.ColumnProvider, nautilus.InfoProvider):

    def __init__(self): pass

    def get_columns(self):
        return (
            nautilus.Column("plone::workflow_status_column",
                            "workflow_status",
                            "Workflow Status",
                            "Show the workflow status of the object in Plone"),
            nautilus.Column("plone::dublincore_title_column",
                            "dc_title",
                            "Title",
                            "Show the Dublin Core Title from Plone"),
            )

    def update_file_info(self, file):
        if file.get_uri_scheme() != 'plone':
            return

        file.add_string_attribute('workflow_status', '')
        file.add_string_attribute('dc_title', '')

        handle = gnomevfs.async.open(
            file.get_uri(), callback=open_cb,
            data=(WF_NS, file, 'review_state', 'workflow_status'))

        handle = gnomevfs.async.open(
            file.get_uri(), callback=open_cb,
            data=(DC_NS, file, 'title', 'dc_title'))
